package com.lzx.iteam.net;

import android.os.Message;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shijunting on 15-5-27.
 */
public class EventMemberMsg extends CcMsgStructure {

    private String mEventType;
    private Message mmCallback;

    public EventMemberMsg(Message mmCallback,String mEventType) {
        this.mmCallback = mmCallback;
        this.mEventType = mEventType;
    }

    public EventMemberMsg() {
    }


    @Override
    public void onReceive(JSONObject data) {
        List<EventMemberMsg> unReadList = new ArrayList<EventMemberMsg>();
        List<EventMemberMsg> readedList = new ArrayList<EventMemberMsg>();
        List<EventMemberMsg> unJoinList = new ArrayList<EventMemberMsg>();
        List<EventMemberMsg> joinList = new ArrayList<EventMemberMsg>();
        Map<String, List<EventMemberMsg>> map = new HashMap<String, List<EventMemberMsg>>();

        try {
            JSONArray jsonArray = data.getJSONArray(AsynHttpClient.KEY_CC_DATA);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                EventMemberMsg bean = new EventMemberMsg();
                bean.setUserId(object.getString("user_id"));
                bean.setUserName(object.getString("user_name"));
                if (object.has("user_image")){
                    bean.setUserImg(object.getString("user_image"));
                }else{
                    bean.setUserImg("");
                }
                bean.setUserType(object.getString("user_type"));
                bean.setHasRead(object.getString("has_read"));
                bean.setId(object.getString("_id"));
                // 判断是不是活动
                if (mEventType.equals("7")){
                    if (object.has("has_join") && "1".equals(object.getString("has_join"))){
                        joinList.add(bean);
                    }else{
                        unJoinList.add(bean);
                    }
                } else if (mEventType.equals("8")){
                    if (object.has("vote")) {
                        readedList.add(bean);
                    } else {
                        unReadList.add(bean);
                    }
                }else if (mEventType.equals("9")){
                    if (object.has("has_join") && !"0".equals(object.getString("has_join"))){
                        joinList.add(bean);
                    }else{
                        unJoinList.add(bean);
                    }
                }else {
                    if ("1".equals(object.getString("has_read"))) {//已读
                        readedList.add(bean);
                    } else {//未读
                        unReadList.add(bean);
                    }
                }

            }
            if (mEventType.equals("7") || mEventType.equals("9")){
                map.put("join",joinList);
                map.put("unJoin",unJoinList);
            }else if (mEventType.equals("8")){
                map.put("vote",readedList);
                map.put("unVote",unReadList);
            }else{
                map.put("unRead",unReadList);
                map.put("readed",readedList);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (mmCallback != null) {
            mmCallback.obj = map;
            /*if ("1".equals(mUserType)) {
                mmCallback.obj = map;
            } else {
                mmCallback.obj = allList;
            }*/
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
        }

    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }
        }
        if(mmCallback != null){
            mmCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mmCallback.obj = errorMsg;
            }else{
                mmCallback.obj = Constants.NETWORK_WRROR;
            }
            mmCallback.sendToTarget();
        }
    }

    private String userId;
    private String id;
    private String userName;
    private String userType;
    private String userImg;
    private String hasRead;//是否已读 1已读

    public Message getMmCallback() {
        return mmCallback;
    }

    public void setMmCallback(Message mmCallback) {
        this.mmCallback = mmCallback;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserType() {
        return userType;
    }

    public void setUserType(String userType) {
        this.userType = userType;
    }

    public String getUserImg() {
        return userImg;
    }

    public void setUserImg(String userImg) {
        this.userImg = userImg;
    }

    public String getHasRead() {
        return hasRead;
    }

    public void setHasRead(String hasRead) {
        this.hasRead = hasRead;
    }
}
